package com.cmccgz.udacs_landlord_webapp.main.user.login;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.baselibrary.base.PermissionUtils;
import com.cmccgz.baselibrary.utils.RegularUtil;
import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.main.host.CHostActivity;
import com.cmccgz.udacs_landlord_webapp.main.host.HostActivity;

/**
 * @author: INX
 * 登陆Activity
 */

public class LoginActivity extends BaseActivity {

    private PermissionUtils.PermissionAskCallback callback = new PermissionUtils.PermissionAskCallback() {
        @Override
        public void hasPermissionDoNext() {

        }
    };

    @Override
    protected int setLayoutId() {
        return 0;
    }

    @Override
    protected void initView() {
        ApiService.init();
        AppConfig.setIMEI(this);
        AppConfig.setIMSI(this);
        String currentMobile = AppConfig.getMobile(mContext);
        if (RegularUtil.isMobileNumber(currentMobile)) {
//            JPushInterface.setAlias(this, 0, currentMobile);
            String userType = AppConfig.getUserType(mContext);
            if (getString(R.string.type_custemer).equals(userType)) {
                startActivity(new Intent(mContext, CHostActivity.class));
            } else {
                startActivity(new Intent(mContext, HostActivity.class));
            }
            finish(false);
            return;
        }
    }

    @Override
    protected void initFragment() {
        LoginFragment loginFragment = (LoginFragment) fragmentManager.findFragmentById(R.id.contentFrame);
        if (loginFragment == null) {
            loginFragment = LoginFragment.newInstance();
            showFragment(loginFragment);
        }
    }

    @Override
    protected void beforePermission() {

    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    /**
     * 忘记密码
     *
     * @param userName
     */
    public void forgetPwd(String userName) {
        PermissionUtils.askPermission(this, 404, callback, Manifest.permission.READ_SMS);
        ForgetPwdFragment forgetPwdFragment = ForgetPwdFragment.instance();
        if (!StringUtils.isEmpty(userName)) {
            Bundle args = new Bundle();
            args.putString("userName", userName);
            forgetPwdFragment.setArguments(args);
        }
        showFragment(forgetPwdFragment);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 404){
            PermissionUtils.handlePermissionRequest(permissions, grantResults, this, callback, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        }
    }
}
